package com.beijiteshop.libcommon.base.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import com.beijiteshop.libcommon.R
import com.beijiteshop.libcommon.databinding.EmptyCallbackBinding
import com.beijiteshop.libcommon.databinding.ErrorCallbackBinding
import com.beijiteshop.libcommon.databinding.LoadCallbackBinding
import com.beijiteshop.libcommon.databinding.NetworkCallbackBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/3 11:16
 * @Description BaseQuickAdapter的扩展函数，展示加载/失败/空/网络错误状态
 */

fun <T,VH: BaseViewHolder>BaseQuickAdapter<T,VH>.showLoading(text: String ?= null){
    val context = this.recyclerView.context
    setEmptyView(
        getLoadingView(
            context,
            text
        )
    )
}

fun <T,VH: BaseViewHolder>BaseQuickAdapter<T,VH>.showError(text: String ?= null,listener: View.() -> Unit){
    val context = this.recyclerView.context
    setEmptyView(
        getErrorView(
            context,
            listener,
            text
        )
    )
}

fun <T,VH: BaseViewHolder>BaseQuickAdapter<T,VH>.showEmpty(text: String ?= null,listener: View.() -> Unit){
    val context = this.recyclerView.context
    setEmptyView(
        getEmptyView(
            context,
            listener,
            text
        )
    )
}

fun <T,VH: BaseViewHolder>BaseQuickAdapter<T,VH>.showNetError(text: String ?= null,listener: View.() -> Unit){
    val context = this.recyclerView.context
    setEmptyView(
        getNetErrorView(
            context,
            listener,
            text
        )
    )
}

private fun getLoadingView(context: Context,text: String?): View {
    val loadView = DataBindingUtil.inflate<LoadCallbackBinding>(LayoutInflater.from(context), R.layout.load_callback,null,false)
    text?.also {
        loadView.loadingTv.text= it
    }
    return loadView.root
}

private fun getErrorView(context: Context,listener: View.() -> Unit,text: String?): View {
    val errorView = DataBindingUtil.inflate<ErrorCallbackBinding>(LayoutInflater.from(context), R.layout.error_callback,null,false)
    text?.also {
        errorView.errorTextTv.text = it
    }
    return errorView.root.apply {
        setOnClickListener {
            listener.invoke(it)
        }
    }
}

private fun getEmptyView(context: Context,listener: View.() -> Unit,text: String?): View {
    val emptyView = DataBindingUtil.inflate<EmptyCallbackBinding>(LayoutInflater.from(context), R.layout.empty_callback,null,false)
    text?.also {
        emptyView.emptyTextTv.text = it
    }
    return emptyView.root.apply {
        setOnClickListener {
            listener.invoke(it)
        }
    }
}

private fun getNetErrorView(context: Context,listener: View.() -> Unit,text: String?): View {
    val netView = DataBindingUtil.inflate<NetworkCallbackBinding>(LayoutInflater.from(context), R.layout.network_callback,null,false)
    text?.also {
        netView.netTextTv.text = it
    }
    return netView.root.apply {
        setOnClickListener {
            listener.invoke(it)
        }
    }
}
